using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Destructiable : MonoBehaviour
{
    public int maxHP = 100;// 最大生命值
    private int currentHP;// 当前生命值
    public List<Sprite> spriteList;//不同阶段的图片
    private SpriteRenderer spriteRenderer;
    private void Start()
    {
        currentHP = maxHP;
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        PlaySFXCollision();
        // 获取当前碰撞的相对速度
        Vector2 relativeVelocity = collision.relativeVelocity;
        // 计算相对速度的大小（标量值）
        float impactForce = relativeVelocity.magnitude;
        Debug.Log(impactForce);
        if(impactForce > 1) TakeDamage((int)impactForce * 8);
    }

    public void TakeDamage(int damage){
        // 根据相对速度大小计算伤害值，并减少当前生命值
        currentHP -= damage;
        if (currentHP <= 0)
        {
            Dead();
        }
        else
        {
            //计算剩余生命值的比例
            float healthRatio = (float)currentHP / maxHP;
            //计算阶段索引
            int index = (int)((1 - healthRatio) * spriteList.Count) - 1;
            if (index != -1) spriteRenderer.sprite = spriteList[index];
        }
    }

    public virtual void Dead(){
        Destroy(gameObject);
        //爆炸特效
        GameObject prefabs = Resources.Load("Prefabs/VFX/爆炸烟雾特效") as GameObject;
        GameObject go = Instantiate(prefabs, transform.position, Quaternion.identity);
        Destroy(go, 1f);
        PlaySFXDie();
    }
    public virtual void PlaySFXCollision(){
        // AudioManager.Instance.PlaySFX("撞木头");
    }
    public virtual void PlaySFXDie(){
        AudioManager.Instance.PlaySFX("木头爆炸");
    }

}
